<template>
	<view>
		<view class="huiyuan-background">
			<view class="huiyuan-jieshao">
			     <view class="container">
    <view class="header">
      <text class="title">会员卡信息</text>
      <text class="subtitle">查看您的会员等级和积分详情</text>
    </view>
    
    <view class="card-container" :class="cardClass">
      <view class="card-header">
        <view class="avatar-container">
          <image :src="memberData.avatar" mode="aspectFill" class="avatar"></image>
          <view class="status-dot">
            <text class="status-icon">✓</text>
          </view>
        </view>
        <view class="user-info">
          <text class="username">{{ memberData.name }}</text>
          <view class="member-type-container">
            <text class="member-type">{{ cardTypeName }}</text>
           <text class="chongzhi" @click="chongzhi()">充值</text>
          </view>
        </view>
      </view>
      
      <view class="card-content">
        <view class="info-item">
          <text class="info-label">积分余额</text>
          <text class="info-value">{{ formattedPoints }} 分</text>
        </view>
        <view class="info-item">
          <text class="info-label">账户余额</text>
          <text class="info-value">¥{{ formattedBalance }}</text>
        </view>
      </view>
      
      <view class="card-footer">
        <view class="next-level">
          <text class="next-level-label">距离下一等级</text>
          <text class="next-level-value">{{ memberData.nextLevelPoints === '顶级会员' ? '已达顶级会员' : memberData.nextLevelPoints + ' 积分' }}</text>
        </view>
        <view class="card-actions">
          <button class="action-btn" hover-class="action-btn-hover">
            <text class="iconfont icon-gift"></text>
          </button>
          <button class="action-btn" hover-class="action-btn-hover">
            <text class="iconfont icon-barcode"></text>
          </button>
          <button class="action-btn" hover-class="action-btn-hover">
            <text class="iconfont icon-qrcode"></text>
          </button>
        </view>
      </view>
    </view>
    
    <view class="privileges-container">
      <text class="privileges-title">会员特权</text>
      <view class="privileges-list">
        <view class="privilege-item" v-for="(privilege, index) in privileges" :key="index">
          <text class="privilege-icon">✓</text>
          <text class="privilege-text">{{ privilege }}</text>
        </view>
      </view>
    </view>
    
    <button class="refresh-btn" @click="refreshMemberData" :disabled="isLoading">
      <text class="refresh-icon" :class="isLoading ? 'icon-loading icon-spin' : 'icon-refresh'"></text>
      <text>{{ isLoading ? '加载中...' : '刷新会员信息' }}</text>
    </button>
  </view>
			</view>
			<view class="cardList">
				<view class="cardList-item" v-for="item in cardList" @click="onchange(item)">
					
						<image :src="item.icon">
							
					</image>
				
				</view>
			</view>
		</view>
	</view>
</template>

<script>
	export default {
		data() {
			return {
				cardList:[
					{
						name:"白金",
						PageUrl:"/pages/baijin/baijin",
						icon:"/static/huiyuanImg/white.png"
					},
					{
						name:"黄金",
						PageUrl:"/pages/huangjin/huangjin",
						icon:"/static/huiyuanImg/yellow.png"
					},
					{
						name:"黑金",
						PageUrl:"/pages/heijing/heijing",
						icon:"/static/huiyuanImg/black.png"
					}
				],
				   memberData: {
				        name: '',
				        avatar: '',
				        memberType: '',
				        points: 0,
				        balance: 0,
				        joinDate: '',
				        nextLevelPoints: ''
				      },
				      cardClass: '',
				      cardTypeName: '',
				      formattedJoinDate: '',
				      formattedPoints: '',
				      formattedBalance: '',
				      privileges: [],
				      isLoading: false
				
				
				
			}
		},
		  onLoad() {
		    this.fetchMemberData();
		  },  
		methods: {
			onchange(a){
				uni.navigateTo({
						
					url:a.PageUrl
				})
				console.log(kk)
			}
			, async fetchMemberData() {
      this.isLoading = true;
      
      try {
        // 实际项目中使用uni.request调用后端API
        // const res = await uni.request({
        //   url: '/api/member/info',
        //   method: 'GET'
        // });
        // this.memberData = res.data.data;
        
        // 模拟API响应
        const memberTypes = ['platinum', 'gold', 'blackgold'];
        const randomType = memberTypes[Math.floor(Math.random() * memberTypes.length)];
        
        this.memberData = {
          name: '张先生',
          avatar: 'https://picsum.photos/id/64/200/200',
          memberType: randomType,
          points: randomType === 'platinum' ? 3000 : 
                  randomType === 'gold' ? 5000 : 10000,
          balance: randomType === 'platinum' ? 300 : 
                  randomType === 'gold' ? 500 : 1000,
          joinDate: '2023-05-15',
          nextLevelPoints: randomType === 'platinum' ? 2000 : 
                          randomType === 'gold' ? 5000 : '顶级会员'
        };
        
        // 处理数据
        this.processMemberData();
        
        // 显示成功提示
        uni.showToast({
          title: '会员信息已更新',
          icon: 'success',
          duration: 2000
        });
      } catch (error) {
        console.error('获取会员数据失败:', error);
        
        // 显示错误提示
        uni.showToast({
          title: '获取会员信息失败，请稍后重试',
          icon: 'none',
          duration: 2000
        });
      } finally {
        this.isLoading = false;
      }
    },
    
    processMemberData() {
      // 设置卡片样式
      switch(this.memberData.memberType) {
        case 'platinum':
          this.cardClass = 'card-platinum';
          this.cardTypeName = '白金卡会员';
          break;
        case 'gold': 
          this.cardClass = 'card-gold';
          this.cardTypeName = '黄金卡会员';
          break;
        case 'blackgold':
          this.cardClass = 'card-blackgold';
          this.cardTypeName = '黑金卡会员';
          break;
        default:
          this.cardClass = 'card-normal';
          this.cardTypeName = '普通会员';
      }
      
      // 格式化日期
      const date = new Date(this.memberData.joinDate);
      this.formattedJoinDate = `${date.getFullYear()}年${date.getMonth() + 1}月${date.getDate()}日`;
      
      // 格式化积分和余额
      this.formattedPoints = this.memberData.points.toLocaleString();
      this.formattedBalance = this.memberData.balance.toLocaleString();
      
      // 设置会员特权
      this.setPrivileges();
    },
    
    setPrivileges() {
      const privilegesMap = {
        platinum: [
          '购物享受9.9折优惠',
          '1000积分兑换商品',

        ],
        gold: [
          '购物享受9.8折优惠',
        
          '专属客服24小时服务',
         
          '3000积分兑换商品'
        ],
        blackgold: [
          '购物享受9.5折优惠',
          
          '专属私人购物顾问',
          '5000积分兑换商品'
        ]
      };
      
      this.privileges = privilegesMap[this.memberData.memberType] || [];
    },
    
    refreshMemberData() {
      this.fetchMemberData();
    },
	chongzhi(){
		uni.navigateTo({
				
			url:"/pages/index"
		})
		console.log(kk)
	}
			
			
			
			
		}
	}
</script>

<style>
	.chongzhi{
		border: #666;
		border-radius: 4px;
		border-style: solid;
		font-size: 20px;
		font-weight: 700;
		width: 50px;
		height: 40px;
		text-align: center;
		line-height: 40px;
		margin-left: 20px;
	}
	
.huiyuan-background{
	height: 700px;
	margin-top: 0%;
	width: 100%;
	background-image: linear-gradient(0deg, #deecf9, #3187f6);
}
.cardList{
	position: absolute;
	width: 100%;
	height:600px ;
	/* background-color: bisque; */
}
.cardList-item{

	background-color: aqua;
	width: 250px;
	height: 150px;
	margin-top: 60px;
	margin-left: 65px;
	border-radius: 20px;
}
image{border-radius: 20px;
	width: 100%;
	height: 100%;
}

.container {
  padding: 20px;
  background-color: #f5f5f5;
  min-height: 100vh;
}

.header {
  text-align: center;
  margin-bottom: 30px;
}

.title {
  font-size: 24px;
  font-weight: bold;
  color: #333;
  display: block;
  margin-bottom: 10px;
}

.subtitle {
  font-size: 14px;
  color: #666;
  display: block;
}

.card-container {
  border-radius: 15px;
  padding: 20px;
  margin-bottom: 30px;
  box-shadow: 0 4px 15px rgba(0, 0, 0, 0.1);
  position: relative;
  overflow: hidden;
}

.card-platinum {
  background: linear-gradient(135deg, #e5e4e2 0%, #f5f5f5 50%, #e5e4e2 100%);
  color: #333;
}

.card-gold {
  background: linear-gradient(135deg, #d4af37 0%, #ffd700 50%, #d4af37 100%);
  color: #333;
}

.card-blackgold {
  background: linear-gradient(135deg, #2a2a2a 0%, #4a4a4a 50%, #2a2a2a 100%);
  color: #fff;
}

.card-normal {
  background: #e9e9e9;
  color: #333;
}

.card-header {
  display: flex;
  align-items: center;
  margin-bottom: 20px;
}

.avatar-container {
  position: relative;
  margin-right: 15px;
}

.avatar {
  width: 60px;
  height: 60px;
  border-radius: 50%;
  border: 2px solid #fff;
  box-shadow: 0 2px 5px rgba(0, 0, 0, 0.1);
}

.status-dot {
  position: absolute;
  bottom: -2px;
  right: -2px;
  width: 18px;
  height: 18px;
  border-radius: 50%;
  background-color: #4cd964;
  display: flex;
  align-items: center;
  justify-content: center;
}

.status-icon {
  color: #fff;
  font-size: 10px;
}

.username {
  font-size: 18px;
  font-weight: bold;
  display: block;
  margin-bottom: 5px;
}

.member-type-container {
  display: flex;
  align-items: center;
}

.member-type {
  background-color: rgba(255, 255, 255, 0.3);
  padding: 2px 8px;
  border-radius: 10px;
  font-size: 12px;
  font-weight: 500;
  margin-right: 10px;
}

.join-date {
  font-size: 12px;
  opacity: 0.8;
}

.card-content {
  display: flex;
  margin-bottom: 20px;
}

.info-item {
  flex: 1;
  background-color: rgba(255, 255, 255, 0.2);
  padding: 10px;
  border-radius: 10px;
  margin: 0 5px;
}

.info-label {
  font-size: 12px;
  opacity: 0.8;
  display: block;
  margin-bottom: 5px;
}

.info-value {
  font-size: 18px;
  font-weight: bold;
  display: block;
}

.card-footer {
  display: flex;
  justify-content: space-between;
  align-items: center;
}

.next-level {
  flex: 1;
}

.next-level-label {
  font-size: 12px;
  opacity: 0.8;
  display: block;
}

.next-level-value {
  font-size: 14px;
  font-weight: 500;
  display: block;
}

.card-actions {
  display: flex;
}

.action-btn {
  width: 30px;
  height: 30px;
  background-color: rgba(255, 255, 255, 0.2);
  border-radius: 50%;
  display: flex;
  align-items: center;
  justify-content: center;
  margin-left: 10px;
}

.action-btn-hover {
  background-color: rgba(255, 255, 255, 0.3);
}

.privileges-container {
  background-color: #fff;
  border-radius: 15px;
  padding: 20px;
  margin-bottom: 30px;
  box-shadow: 0 2px 5px rgba(0, 0, 0, 0.1);
}

.privileges-title {
  font-size: 18px;
  font-weight: bold;
  color: #333;
  display: block;
  margin-bottom: 15px;
}

.privileges-list {
  display: flex;
  flex-direction: column;
}

.privilege-item {
  display: flex;
  align-items: flex-start;
  margin-bottom: 10px;
}

.privilege-icon {
  color: #4cd964;
  font-size: 14px;
  margin-right: 8px;
  margin-top: 2px;
}

.privilege-text {
  font-size: 14px;
  color: #333;
}

.refresh-btn {
  width: 100%;
  background-color: #007aff;
  color: #fff;
  border-radius: 10px;
  padding: 12px 0;
  font-size: 16px;
  display: flex;
  align-items: center;
  justify-content: center;
}

.refresh-btn[disabled] {
  opacity: 0.5;
}

.refresh-icon {
  margin-right: 8px;
}

.icon-spin {
  animation: spin 1s linear infinite;
}

@keyframes spin {
  from { transform: rotate(0deg); }
  to { transform: rotate(360deg); }
}
</style>
